package com.ourteam.workspace.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;

import com.ourteam.scm.utils.ISCMService;
import com.ourteam.scm.utils.ModuleInfoBean;
import com.ourteam.scm.utils.SCMServerBean;
import com.ourteam.scm.utils.SCMServiceFactory;
import com.ourteam.scm.utils.ShowResourceLogConfig;
import com.ourteam.workspace.domain.WorkspaceFileTypeConfigBean;
import com.ourteam.workspace.domain.WorkspaceScmConfigBean;
import com.ourteam.workspace.service.IWorkspaceService;
import com.ourteam.workspace.service.WorkspaceServiceFactory;

public class WorkspaceUtils {

	private static final IWorkspaceService WORKSPACE_SERVICE = WorkspaceServiceFactory.getWorkspaceService();

	public static SCMServerBean getScmServerConfig(long workspaceId) throws Exception {

		WorkspaceScmConfigBean scmConfigBean = WORKSPACE_SERVICE.getWorkspaceScmConfig(workspaceId);

		return getScmServerConfig(scmConfigBean);
	}

	public static SCMServerBean getScmServerConfig(WorkspaceScmConfigBean scmConfigBean) throws Exception {

		SCMServerBean scmServer = new SCMServerBean();

		scmServer.setProtocol(scmConfigBean.getProtocol());

		scmServer.setHostName(scmConfigBean.getHostName());

		scmServer.setHostPort(scmConfigBean.getHostPort());

		scmServer.setScmType(scmConfigBean.getScmType());

		scmServer.setUser(scmConfigBean.getScmUser());

		scmServer.setPassword(scmConfigBean.getScmPassword());

		scmServer.setModuleName(scmConfigBean.getModule());

		return scmServer;
	}

	public static ModuleInfoBean getModuleInfo(long workspaceId) throws Exception {
		WorkspaceScmConfigBean scmConfigBean = WORKSPACE_SERVICE.getWorkspaceScmConfig(workspaceId);

		return getModuleInfo(scmConfigBean);
	}

	public static ModuleInfoBean getModuleInfo(WorkspaceScmConfigBean scmConfigBean) {
		ModuleInfoBean moduleInfoBean = new ModuleInfoBean();

		moduleInfoBean.setBranch(scmConfigBean.getBranch());
		moduleInfoBean.setLabel(scmConfigBean.getLabel());

		return moduleInfoBean;
	}

	public static ISCMService getSCMService(long workspaceId) throws Exception {

		return SCMServiceFactory.getSCMService(getScmServerConfig(workspaceId));

	}

	public static ISCMService getSCMService(WorkspaceScmConfigBean scmConfigBean) throws Exception {

		return SCMServiceFactory.getSCMService(getScmServerConfig(scmConfigBean));

	}

	public static Map<String, WorkspaceFileTypeConfigBean> getWorkspaceFileTypeConfigMap() throws Exception {

		Map<String, WorkspaceFileTypeConfigBean> map = new HashMap<>();

		WorkspaceFileTypeConfigBean[] fileTypeConfigBeans = WORKSPACE_SERVICE.queryWorkspaceFileTypeConfigs(null);

		for (int i = 0; i < fileTypeConfigBeans.length; i++) {
			WorkspaceFileTypeConfigBean workspaceFileTypeConfigBean = fileTypeConfigBeans[i];
			map.put(workspaceFileTypeConfigBean.getFileExtension(), workspaceFileTypeConfigBean);
		}

		return map;

	}

	public static ShowResourceLogConfig[] getWorkspaceFileTypeConfigs() throws Exception {

		List<ShowResourceLogConfig> configs = new ArrayList<>();

		WorkspaceFileTypeConfigBean[] fileTypeConfigBeans = WORKSPACE_SERVICE.queryWorkspaceFileTypeConfigs(null);

		for (int i = 0; i < fileTypeConfigBeans.length; i++) {
			WorkspaceFileTypeConfigBean workspaceFileTypeConfigBean = fileTypeConfigBeans[i];
			ShowResourceLogConfig config = new ShowResourceLogConfig();

			config.setFileExtension(workspaceFileTypeConfigBean.getFileExtension());

			config.setCountLine(BooleanUtils.toBoolean(workspaceFileTypeConfigBean.getIsCountLine()));

			config.setShowDiff(BooleanUtils.toBoolean(workspaceFileTypeConfigBean.getIsCanDiff()));

			configs.add(config);
		}

		return configs.toArray(new ShowResourceLogConfig[configs.size()]);

	}

	public static String getSVNUrl(WorkspaceScmConfigBean scmConfigBean, ModuleInfoBean moduleInfo) throws Exception {
		StringBuffer url = new StringBuffer();

		SCMServerBean scmServerBean = getScmServerConfig(scmConfigBean);

		url.append(scmServerBean.getProtocol()).append("://");
		url.append(scmServerBean.getHostName());
		if (StringUtils.isNotBlank(scmServerBean.getHostPort())) {
			url.append(":").append(scmServerBean.getHostPort());
		}
		if (StringUtils.startsWith(scmServerBean.getServiceName(), "/")) {
			url.append(scmServerBean.getServiceName());
		} else {
			url.append("/").append(scmServerBean.getServiceName());
		}

		if (moduleInfo != null) {

			if (StringUtils.isNotBlank(moduleInfo.getModule())) {

				if (moduleInfo.getModule().startsWith("/") == false) {
					url.append("/");
				}

				url.append(moduleInfo.getModule());
			}
		}

		return url.toString();
	}

}
